package app.auto;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.aliang.auto.R;

import app.auto.runner.base.widget.ImageView;
import app.auto.runner.base.widget.ObservableScrollView;

public class ObservableWebViewUtil extends AndroidInstance implements ObservableScrollView.ScrollViewListener {
    public ImageView imageView;
    public TextView textview;
    private int imageHeight;
    private ObservableScrollView scrollView;
    private Context ctx;
    private TextView textView;


    View v;
    ViewGroup container;

    public ObservableWebViewUtil init() {
        v = LayoutInflater.from(getContext()).inflate(R.layout.displayscroll, null);

        imageView = v.findViewById(R.id.imageview);
        textview = v.findViewById(R.id.textview);
        container = scrollView = v.findViewById(R.id.scrollview);
        initListeners();
        return this;
    }

    public ObservableWebViewUtil addViewtoContainer(ViewGroup vg, View v) {
        vg.addView(v);
        return this;
    }

    public void initListeners() {
        // 获取顶部图片高度后，设置滚动监听
        ViewTreeObserver vto = imageView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                imageView.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                imageHeight = imageView.getHeight();

                scrollView.setScrollViewListener(ObservableWebViewUtil.this);
            }
        });
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
                                int oldx, int oldy) {
        // TODO Auto-generated method stub
        // Log.i("TAG", "y--->" + y + "    height-->" + height);
        if (y <= 0) {
            textView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//第一个参数0表示透明度范围是0~255，后面为RGB参数
        } else if (y > 0 && y <= imageHeight) {
            float scale = (float) y / imageHeight;
            float alpha = (255 * scale);
            textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));
        } else {
            textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));
        }
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }
}
